<?php
require_once 'Zend/Controller/Action.php';

class Admin_EmailController extends Zend_Controller_Action 
{
    public function indexAction()
    {
        $params = array(
            'host'      => 'pop2.bysoft.fr',
            'user'      => 'kim.jin@bysoft.fr',
            'password'  => 'kjn97*',
            'port'      => '110',
        );
        $mailStorageRemote = new Zend_Mail_Storage_Pop3($params);

        $count = $mailStorageRemote->countMessages();
        $this->view->countMailsRemote = $count;
        if ($count > 0) {
            $this->view->mailsRemote = $mailStorageRemote;
        }

    }

    public function viewAction()
    {
        if ($this->getRequest()->isGet()) {
            $id = $this->_getParam('id');

            if (intval($id)) {
                $params = array(
                    'host'      => 'pop2.bysoft.fr',
                    'user'      => 'kim.jin@bysoft.fr',
                    'password'  => 'kjn97*',
                    'port'      => '143',
                    //'folder'    => 'INBOX',
                );
                $mailStorage = new Zend_Mail_Storage_Imap($params);

                $this->view->mailFolders = $mailStorage->getFolders();

                $count = $mailStorage->countMessages();
                
                if ($count > 0) {
                    $this->view->mail = $mailStorage->getMessage($id);
                }

            } else {
                $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
                $flashMessenger->setNamespace('post')->addMessage($filterResult);
                $this->_redirect('/admin/post/');
            }
        }
    }

    public function editAction()
    {}

    public function sendAction()
    {
        $redirector = $this->getHelper('Redirector');

        if ($this->getRequest()->isPost()) {

            $config = Kbs::initConfig('Mail');

            $params = $this->_getAllParams();

            $filterFormat = new Zend_Filter;

            $email_from = $filterFormat->filter($params['email_from']);
            $email_to = $params['email_to'];
            $email_cc = $params['email_cc'];
            $email_bcc = $params['email_bcc'];
            $email_subject = $filterFormat->filter($params['email_subject']);
            $email_body = $filterFormat->filter($params['email_body']);
            $token = $params['token'];

            $validator = new Zend_Validate_EmailAddress();

            $mail = Kbs::initMail();

            if ($validator->isValid($email_to)) {
                $mail->addTo($email_to, $email_to);
            }
            
            if ($validator->isValid($email_cc)) {
                $mail->addCc($email_cc, $email_cc);
            }

            if ($validator->isValid($email_bcc)) {
                $mail->addBcc($email_bcc, $email_bcc);
            }

            if (isset($config->headers)) {
                foreach ($config->headers as $header => $value) {
                    $mail->addHeader($header, $value, true);
                }
            }

            $mail->setFrom($email_from, 'Support');
            $mail->setSubject($email_subject);
            //$mail->setBodyText($email_body);
            $mail->setBodyHtml($email_body);
        
            $error = false;
            
            /*
            Zend_Loader::loadClass('Zend_File_Transfer_Adapter_Http');
            $fileTransfer = new Zend_File_Transfer_Adapter_Http();
            $fileTransfer->setDestination(Zend_Registry::get('pathUploads'));
            $fileTransfer->addValidator('Size', 20000);
            if ($fileTransfer->isValid() and $fileTransfer->receive() !== false) {
            }
            */

            if (is_uploaded_file($_FILES['email_attachment']['tmp_name'])) {
                $fileName = $config->attachment->pathUploads . $_FILES['email_attachment']['name'];
                if (move_uploaded_file($_FILES['email_attachment']['tmp_name'], $fileName)) {
                    $at = $mail->createAttachment(file_get_contents($fileName));
                    $at->filename = $_FILES['email_attachment']['name'];

                    if (isset($config->attachment->encoding)) {
                        $at->encoding = $config->attachment->encoding;
                    }
                    
                } else {
                    $error = true;
                }
            }

            if ($error) {
                $flashMessenger = $this->getHelper('FlashMessenger');
                $flashMessenger->setNamespace('email')->addMessage('email error !');
                $redirector->gotoUrl('/email/edit');
            } else {
                $mail->send();
            }
        }

        $redirector->gotoUrl('/admin/email');
    }

    public function listAction()
    {}



}